<?php

namespace Application\Controller;

use Zend\View\Model\ViewModel;
use Admin\Model\DBO\ArticleDBO;
use Application\Controller\IndexController;

class ViewArticleController extends IndexController {

    public function indexAction() {
        // Neu dang nhap roi thi hien thi len sach sach nguoi dung
        // Lay thuc the dang nhap dua ra layout
        $objectArticleDBO = new ArticleDBO($this->getServiceLocator());
        // Lay ở mục tin tức/ tin tức
        $paginator = $objectArticleDBO->getAllArticlePublish(1, 1);
        $paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page', 1));
        $paginator->setItemCountPerPage(5);
        $paginator->setPageRange(3);

        return array(
            'paginator' => $paginator,
        );
    }

    public function articleDetailAction() {
        $articleID = $this->params()->fromRoute('id', 0);
        if (!$articleID) {
            return $this->redirect()->toRoute('view-article');
        }
        $objectArticleDBO = new ArticleDBO($this->getServiceLocator());
        $article = $objectArticleDBO->getArticle($articleID);
        $tmp = $article->dateCreated;
        $dateCreated = new \DateTime($tmp);
        $listNewArticle = $objectArticleDBO->getListNewArticle(1, 1, 5, $dateCreated->format('Y-m-d'));
        return array(
            'article' => $article,
            'listNewArticle' => $listNewArticle,
        );
    }

    public function announcementAction() {
        $objectArticleDBO = new ArticleDBO($this->getServiceLocator());
        $announcement = $objectArticleDBO->getNewArticle(1, 2, 1);
        // lay danh sach cac thong bao moi
        $tmp = null;
        foreach ($announcement as $row) {
            $tmp = $row->dateCreated;
        }
        $dateCreated = new \DateTime($tmp);
        $listNewAnnouncement = $objectArticleDBO->getListNewArticle(1, 2, 5, $dateCreated->format('Y-m-d'));
        return array(
            'announcement' => $announcement,
            'listNewAnnouncement' => $listNewAnnouncement,
        );
    }

    public function announcementDetailAction() {
        $articleID = $this->params()->fromRoute('id', 0);
        if (!$articleID) {
            return $this->redirect()->toRoute('view-article/process', array('action' => 'announcement'));
        }
        $objectArticleDBO = new ArticleDBO($this->getServiceLocator());
        $announcement = $objectArticleDBO->getArticle($articleID);
        $tmp = $announcement->dateCreated;
        $dateCreated = new \DateTime($tmp);
        $listNewAnnouncement = $objectArticleDBO->getListNewArticle(1, 2, 5, $dateCreated->format('Y-m-d'));
        return array(
            'announcement' => $announcement,
            'listNewAnnouncement' => $listNewAnnouncement,
        );
    }

}